package cz.muni.fi.pa165.sportclubmanager.service;

import cz.muni.fi.pa165.sportclubmanager.dao.PlayerDAO;
import cz.muni.fi.pa165.sportclubmanager.dto.PlayerDTO;
import cz.muni.fi.pa165.sportclubmanager.dto.PlayerDTOMapping;
import cz.muni.fi.pa165.sportclubmanager.entity.Player;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;


@Stateless
@TransactionManagement
public class PlayerServiceImpl implements PlayerService {
    
    @EJB
    private PlayerDAO playerDAO;
           
    @Override
    public void create(PlayerDTO p) {
        playerDAO.create(PlayerDTOMapping.toEntity(p));        
    }

    @Override
    public void update(PlayerDTO p) {
        playerDAO.update(PlayerDTOMapping.toEntity(p));                
    }

    @Override
    public void remove(PlayerDTO p) {
        playerDAO.remove(PlayerDTOMapping.toEntity(p));                        
    }

    @Override
    public PlayerDTO find(Long id) {
        Player p = playerDAO.find(id);                                        
        
        return PlayerDTOMapping.toDto(p);
    }

    @Override
    public List<PlayerDTO> findAll() {
        List<Player> list = playerDAO.findAll();
        List<PlayerDTO> dtoList = new ArrayList<PlayerDTO>();
                
        for (Iterator i = list.iterator(); i.hasNext();) {            
            dtoList.add(PlayerDTOMapping.toDto((Player) i.next()));
        }
        return dtoList;
    }
        
}
